/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model.Servicios;

import Model.Beans.Institution;
import Model.Beans.User;
import Model.DAO.InstitutionDAO;
import Model.DAO.UserDAO;
import java.util.List;

/**
 *
 * @author John Mex
 */
public class InstSrv {
    private InstitutionDAO instDAO  = new InstitutionDAO();     
    private UserDAO userDAO = new UserDAO();
    
    public void addInstitution(Institution inst){
        userDAO.insertar(inst.getUser());
        instDAO.insertar(inst);
    }
    
    public boolean usernameExist(String username)
    {
        List<User> institutions;
        institutions = userDAO.buscar("username='"+username+"'", User.class);
        if(institutions.isEmpty()){
            return false;
        }
        return true;
        
    }
    
    public List<Institution> gellAll(){
         List<Institution> institutions =instDAO.getAll(Institution.class);
        return institutions;
    }
    
    public Institution getInstitution(int institutionId){
        
        List<Institution> institutions = instDAO.buscar("idinstitution="+institutionId, Institution.class);
        if(!institutions.isEmpty())
        {
             return instDAO.getInstitution(institutions.get(0));
        }       
       return null;

    }

    public Institution getInstitution(String institution) {
         List<Institution> institutions = instDAO.buscar("name="+institution, Institution.class);
        if(!institutions.isEmpty())
        {
             return instDAO.getInstitution(institutions.get(0));
        }
       return null;
    }
    
    public void deleteInstitutionById(String institutionId) {
        System.out.println("InstitutionId: " + institutionId);
        Institution institutionToDelete = getInstitutionById(institutionId);
        User userToDelete = institutionToDelete.getUser();
        if (institutionToDelete != null) {
            System.out.println("InstitutionId: " + institutionToDelete.getUser().getPassword());
            instDAO.borrar(institutionToDelete);
            new UserDAO().borrar(userToDelete);
        }
        else {
            System.out.println("InstitutionId: null");
        }
    }
    
    public Institution getInstitutionById(String institutionId){
        List<Institution> students = instDAO.buscarInstitution(institutionId);
        return (!students.isEmpty()) ? students.get(0) : null;
    }
    
    public void updateInstitution(Institution institution) {
        instDAO.actualizar(institution);
    }
}
